package com.itboye.pondteam.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * @Author:Create by Mr.w
 * @Date:2018/12/17 9:41
 * @Description:
 */
public class StatisticalDurationDbManager {


    private Context mContext;
    private StatisticalDurationHelper mDbHelper;
    private SQLiteDatabase db;

    public StatisticalDurationDbManager(Context context) {
        mContext = context;
        mDbHelper = new StatisticalDurationHelper(context);
    }

    public long addTime(String uid, long time, String visit_date) {
        db = mDbHelper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("time", time);
        cv.put("uid", uid);
        cv.put("visit_date", visit_date);
        long num = db.insert(mDbHelper.TABLENAME_Video, null, cv);
        return num;
    }

    public long updateTime(String uid, long time, String visit_date) {
        db = mDbHelper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("time", time);
        long num = db.update(mDbHelper.TABLENAME_Video, cv, " uid=? and visit_date=?", new String[]{uid, visit_date});
        return num;
    }

    public long deleteTime(String uid) {
        db = mDbHelper.getWritableDatabase();
        long num = db.delete(mDbHelper.TABLENAME_Video, "uid=?", new String[]{uid});
        return num;
    }

    public long queryTime(String uid, String visit_date) {
        db = mDbHelper.getReadableDatabase();
        long totalTime = 0;
        Cursor cursor = null;
        if (this.db != null) {
            cursor = db.query(mDbHelper.TABLENAME_Video, new String[]{"time"}, "uid=? and visit_date=?", new String[]{uid, visit_date}, null, null, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    totalTime += cursor.getLong(0);
                }
            }
        }
        return totalTime;
    }

    //查询某日期是否有观看记录
    public long queryVisitDateCount(String visitDate) {
        db = mDbHelper.getReadableDatabase();
        long totalTime = 0;
        Cursor cursor = null;
        if (this.db != null) {
            cursor = db.query(mDbHelper.TABLENAME_Video, null, "visit_date=?", new String[]{visitDate}, null, null, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    totalTime += cursor.getLong(0);
                }
            }
        }
        return totalTime;
    }


}
